﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

/// <summary>
/// GameManager管理游戏流程，不应该对游戏逻辑做处理，例如资源的管理，场景管理，资源回收等
/// </summary>
public class GameManager : MonoBehaviour
{

    public Transform[] cave = new Transform[5];
    public GameObject[] monster = new GameObject[3];//0crab1cat2bat
    public ShakeCamera mainCamera;

    public int stageNumber = 0;

    private GameObject a;
    private GameObject b;
    private GameObject c;
    private GameObject d;
    private GameObject e;

    private StageClearUI stageClearUIComponent;
    private GameObject stageClearUI;
    private StrengtController strengthController;
    private Transform showStageNumber;
    public int MonsterCount;
    private Transform EndStory;
    private Text EndStoryTalk;
    private SpriteRenderer bigEvil;

    /// <summary>
    /// 游戏管理器全局存在，永远不销毁
    /// </summary>
    void DoNotDestroyOnLoad()
    {

    }
    /// <summary>
    /// 初始化各类管理器
    /// </summary>
    private void Awake()
    {
        
    }

    void Start()
    {
        stageClearUI = GameObject.Find("Canvas").transform.Find("StageClearUI").gameObject;
        stageClearUIComponent = stageClearUI.GetComponent<StageClearUI>();
        strengthController = GameObject.Find("Strengthen").GetComponent<StrengtController>();
        showStageNumber = GameObject.Find("Canvas").transform.Find("StageNumber");
        EndStory = GameObject.Find("Canvas").transform.Find("EndStory");
        EndStoryTalk = EndStory.Find("talk").gameObject.GetComponent<Text>();
        bigEvil = GameObject.Find("BigEvil").GetComponent<SpriteRenderer>();
        StartCoroutine(GameLoop());
	}


    public IEnumerator GameLoop()
    {
        yield return StartCoroutine(StageStart());
        yield return StartCoroutine(StagePlay());
        yield return StartCoroutine(StageEnd());
        StartCoroutine(GameLoop());
    }

    private IEnumerator StageStart()
    {
        stageNumber++;
        if(stageNumber<=7)
        {
            showStageNumber.gameObject.SetActive(true);
            yield return new WaitForSeconds(3f);
            showStageNumber.gameObject.SetActive(false);
        }

    }//关卡开始前

    private IEnumerator StagePlay()
    {
        if (stageNumber == 1)
        {
            int monster1 = Random.Range(0, 2);
            int cave1 = Random.Range(0, 5);
            a = ObjectPool.Instance.OutPool(monster[monster1]);
            a.transform.position = cave[cave1].position;
        }

        if (stageNumber == 2)
        {
            int monster1 = Random.Range(0, 2);
            int monster2 = Random.Range(0, 2);
            int cave1 = Random.Range(0, 3);
            int cave2 = Random.Range(3, 5);
            a = ObjectPool.Instance.OutPool(monster[monster1]);
            a.transform.position = cave[cave1].position;
            b = ObjectPool.Instance.OutPool(monster[monster2]);
            b.transform.position = cave[cave2].position;
        }

        if (stageNumber == 3)
        {
            int cave1 = Random.Range(0, 3);
            int cave2 = Random.Range(3, 5);
            a = ObjectPool.Instance.OutPool(monster[2]);
            a.transform.position = cave[cave1].position;
            b = ObjectPool.Instance.OutPool(monster[2]);
            b.transform.position = cave[cave2].position;
        }

        if (stageNumber == 4)
        {
            int monster1 = Random.Range(0, 2);
            int monster2 = Random.Range(0, 2);
            int monster3 = Random.Range(0, 2);
            int cave1 = Random.Range(0, 2);
            int cave2 = Random.Range(2, 4);
            int cave3 = Random.Range(4, 5);
            a = ObjectPool.Instance.OutPool(monster[monster1]);
            a.transform.position = cave[cave1].position;
            b = ObjectPool.Instance.OutPool(monster[monster2]);
            b.transform.position = cave[cave2].position;
            c = ObjectPool.Instance.OutPool(monster[monster3]);
            c.transform.position = cave[cave3].position;
        }

        if (stageNumber == 5 || stageNumber == 6)
        {
            int monster1 = Random.Range(0, 2);
            int monster2 = Random.Range(0, 2);
            int monster3 = Random.Range(0, 2);
            int monster4 = Random.Range(0, 2);
            int cave1 = Random.Range(0, 1);
            int cave2 = Random.Range(1, 2);
            int cave3 = Random.Range(2, 4);
            int cave4 = Random.Range(4, 5);
            a = ObjectPool.Instance.OutPool(monster[monster1]);
            a.transform.position = cave[cave1].position;
            b = ObjectPool.Instance.OutPool(monster[monster2]);
            b.transform.position = cave[cave2].position;
            c = ObjectPool.Instance.OutPool(monster[monster2]);
            c.transform.position = cave[cave3].position;
        }

        if (stageNumber == 7)
        {
            a = ObjectPool.Instance.OutPool(monster[2]);
            a.transform.position = cave[0].position;
            b = ObjectPool.Instance.OutPool(monster[2]);
            b.transform.position = cave[1].position;
            c = ObjectPool.Instance.OutPool(monster[2]);
            c.transform.position = cave[2].position;
            d = ObjectPool.Instance.OutPool(monster[2]);
            d.transform.position = cave[3].position;
            e = ObjectPool.Instance.OutPool(monster[2]);
            e.transform.position = cave[4].position;
        }

        while (!StageClearTest())
        {
            yield return null;
        }

    }//关卡开始时

    private IEnumerator StageEnd()
    {
        if(stageNumber == 1)
        {
            stageClearUI.SetActive(true);
            stageClearUIComponent.SetDescription("魔鬼代理人：\n恭喜你活过了第一波，这可真是一个脏乱差的活不是吗？要不要稍微增强一下自己的能力呢？不不不我不是说你很垃圾的意思，只是来谈一笔小小的交易...额，比如要不要增强一下自己的弹跳引擎呢？这样你就可以酷炫的二段跳了！");
            while(stageClearUIComponent.ChoiceOption==0)
            {
                yield return null;
            }
            if (stageClearUIComponent.ChoiceOption == 1)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n真棒！你做了一个明智的选择！好的，交易已经完成了，我需要去处理下一个客户了...祝你好运！");
                strengthController.IsDoubleJump = true;
            }
            if (stageClearUIComponent.ChoiceOption == 2)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n见鬼，今天一张订单都没有，只能回家喝西北风了。");
            }
            yield return new WaitForSeconds(3f);
            stageClearUIComponent.ChoiceOption = 0;
            stageClearUI.SetActive(false);
        }

        if(stageNumber == 2)
        {
            stageClearUI.SetActive(true);
            stageClearUIComponent.SetDescription("魔鬼代理人：\n嗨老伙汁，我们又见面了，这次我给你带来了不错的东西哟。瞧！快速发射喷嘴，可以让枪的射速飞一般的顺滑，是否考虑一下呢？");
            while (stageClearUIComponent.ChoiceOption == 0)
            {
                yield return null;
            }
            if (stageClearUIComponent.ChoiceOption == 1)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n真棒！你做了一个明智的选择！好的，交易已经完成了，我需要去处理下一个客户了...祝你好运！");
                strengthController.IsStrengthenShootSpeed = true;
            }
            if (stageClearUIComponent.ChoiceOption == 2)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n见鬼，今天一张订单都没有，只能回家喝西北风了。");
            }
            yield return new WaitForSeconds(3f);
            stageClearUIComponent.ChoiceOption = 0;
            stageClearUI.SetActive(false);
        }

        if(stageNumber == 3)
        {
            stageClearUI.SetActive(true);
            stageClearUIComponent.SetDescription("魔鬼代理人：\n这次带来的是血量恢复器具，可以让你马上血量回满，小老弟有没有性趣？");
            while (stageClearUIComponent.ChoiceOption == 0)
            {
                yield return null;
            }
            if (stageClearUIComponent.ChoiceOption == 1)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n真棒！你做了一个明智的选择！好的，交易已经完成了，我需要去处理下一个客户了...祝你好运！");
                strengthController.IsDoubleHealth = true;
            }
            if (stageClearUIComponent.ChoiceOption == 2)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n见鬼，今天一张订单都没有，只能回家喝西北风了。");
            }
            yield return new WaitForSeconds(3f);
            stageClearUIComponent.ChoiceOption = 0;
            stageClearUI.SetActive(false);
        }

        if (stageNumber == 4)
        {
            stageClearUI.SetActive(true);
            stageClearUIComponent.SetDescription("魔鬼代理人：\n哇你竟然坚持到了现在，这次给你准备了一个大礼：绝世罕见的吸血鬼基因！要接种这个基因变成一个酷酷的拉风吸血鬼吗？");
            while (stageClearUIComponent.ChoiceOption == 0)
            {
                yield return null;
            }
            if (stageClearUIComponent.ChoiceOption == 1)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n真棒！你做了一个明智的选择！好的，交易已经完成了，我需要去处理下一个客户了...祝你好运！");
                strengthController.IsVampire = true;
            }
            if (stageClearUIComponent.ChoiceOption == 2)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n见鬼，今天一张订单都没有，只能回家喝西北风了。");
            }
            yield return new WaitForSeconds(3f);
            stageClearUIComponent.ChoiceOption = 0;
            stageClearUI.SetActive(false);
        }

        if (stageNumber == 5)
        {
            stageClearUI.SetActive(true);
            stageClearUIComponent.SetDescription("魔鬼代理人：\n你没有挂掉啊，真是让人失...叫人开心，请不要用枪指着我谢谢。好了看看这次的商品：大口径弹药，一发顶过去两发，要不要来一发？");
            while (stageClearUIComponent.ChoiceOption == 0)
            {
                yield return null;
            }
            if (stageClearUIComponent.ChoiceOption == 1)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n真棒！你做了一个明智的选择！好的，交易已经完成了，我需要去处理下一个客户了...祝你好运！");
                strengthController.IsStengthenShootDamage = true;
            }
            if (stageClearUIComponent.ChoiceOption == 2)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n见鬼，今天一张订单都没有，只能回家喝西北风了。");
            }
            yield return new WaitForSeconds(3f);
            stageClearUIComponent.ChoiceOption = 0;
            stageClearUI.SetActive(false);
        }

        if (stageNumber == 6)
        {
            stageClearUI.SetActive(true);
            stageClearUIComponent.SetDescription("魔鬼代理人：\n若我没记错的话下一个房间就是最后一间了，我可以帮你把弹跳引擎升到满级，让你可以无限连跳，就像一直真正的苍蝇！不过这东西的价格可不便宜，要吗？");
            while (stageClearUIComponent.ChoiceOption == 0)
            {
                yield return null;
            }
            if (stageClearUIComponent.ChoiceOption == 1)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n额好像因为我把这个贵重的东西卖给你惹某些东西生气了，祝你好运，我先溜了！");
                strengthController.IsinfiniteJump = true;
            }
            if (stageClearUIComponent.ChoiceOption == 2)
            {
                stageClearUIComponent.SetDescription("魔鬼代理人：\n见鬼，今天一张订单都没有，只能回家喝西北风了。");
            }
            yield return new WaitForSeconds(3f);
            stageClearUIComponent.ChoiceOption = 0;
            stageClearUI.SetActive(false);
        }

        if (stageNumber == 7)
        {
            StartCoroutine(EndTalk());           
        }


        yield return new WaitForSeconds(2f);
    }//关卡结束后

    private bool StageClearTest()
    {
        MonsterCount = Data.Instance.MonsterCount;
        if (Data.Instance.MonsterCount > 0)
            return false;
        else
            return true;
    }


    private IEnumerator EndTalk()
    {
        mainCamera.shakeLevel++;
        mainCamera.shake();
        mainCamera.shake();
        mainCamera.shake();
        yield return new WaitForSeconds(1);
        bigEvil.color = new Color(bigEvil.color.r, bigEvil.color.g, bigEvil.color.b, 60);
        mainCamera.shakeLevel++;
        mainCamera.shake();
        mainCamera.shake();
        mainCamera.shake();
        bigEvil.color = new Color(bigEvil.color.r, bigEvil.color.g, bigEvil.color.b, 120);
        yield return new WaitForSeconds(1);
        mainCamera.shakeLevel++;
        mainCamera.shake();
        mainCamera.shake();
        mainCamera.shake();
        bigEvil.color = new Color(bigEvil.color.r, bigEvil.color.g, bigEvil.color.b, 180);
        mainCamera.shakeLevel++;
        mainCamera.shake();
        mainCamera.shake();
        mainCamera.shake();
        bigEvil.color = new Color(bigEvil.color.r, bigEvil.color.g, bigEvil.color.b, 255);
        EndStory.gameObject.SetActive(true);
        AudioController.StopAll();
        EndStoryTalk.text = "???:\n \n干得漂亮，萨姆丝。";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "萨姆丝:\n \n谢谢，伊万·伊万诺维奇·伊万诺夫·吃耗子不吐老鼠皮魔鬼大人。任务我已经完成了，请支付我酬劳把。";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "伊万·伊万诺维奇·伊万诺夫·吃耗子不吐老鼠皮恶魔:\n \n可是你杀了我的皮皮。";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "萨姆丝:\n \n？？？嘛玩意？";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "伊万·伊万诺维奇·伊万诺夫·吃耗子不吐老鼠皮:\n \n你杀的第12只猫，是我的宠物猫。你的薪水没了。";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "萨姆丝:\n \n...NMB";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "伊万·伊万诺维奇·伊万诺夫·吃耗子不吐老鼠皮:\n \n你说啥？";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "萨姆丝:\n \n没什么，再见。";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "萨姆丝:\n \n果然跟恶魔做交易的都是傻波依。";
        yield return new WaitForSeconds(3f);
        EndStoryTalk.text = "恭喜！你已经通关啦！即将返回开始界面";
        MonsterBase.SetBuff();
        yield return new WaitForSeconds(5f);
        SceneManager.LoadSceneAsync(0);
    }
}
